package com.rong.raft;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 * @desc raft
 *
 * @author liurong
 *
 * @date 2019年10月21日
 */
public class NodeState {

	// 当前节点角色
	private NodeRoleEnum role = NodeRoleEnum.FOLLOWER;

	// 主节点id
	private String leaderId;

	// 自身节点id
	private String slefId;

	// 当前term
	private long currTerm = 0;

	// 当前投票的node
	private String currVoteFor;

	// node列表
	private Map<String, String> nodeMap = new ConcurrentHashMap<String, String>();

	private String currNodeAddress;

	public NodeRoleEnum getRole() {
		return role;
	}

	public void setRole(NodeRoleEnum role) {
		this.role = role;
	}

	public String getLeaderId() {
		return leaderId;
	}

	public void setLeaderId(String leaderId) {
		this.leaderId = leaderId;
	}

	public long getCurrTerm() {
		return currTerm;
	}

	public void setCurrTerm(long currTerm) {
		this.currTerm = currTerm;
	}

	public String getSlefId() {
		return slefId;
	}

	public void setSlefId(String slefId) {
		this.slefId = slefId;
	}

	public Map<String, String> getNodeMap() {
		return nodeMap;
	}

	public void setNodeMap(Map<String, String> nodeMap) {
		this.nodeMap = nodeMap;
	}

	public String getCurrVoteFor() {
		return currVoteFor;
	}

	public void setCurrVoteFor(String currVoteFor) {
		this.currVoteFor = currVoteFor;
	}

	public String getCurrNodeAddress() {
		return currNodeAddress;
	}

	public void setCurrNodeAddress(String currNodeAddress) {
		this.currNodeAddress = currNodeAddress;
	}

	public void addNode(String nodeId, String address) {
		this.nodeMap.put(nodeId, address);
	}

	public boolean isSelector(int size) {
		return size > nodeMap.size() / 2;
	}

	public long nextTerm() {
		this.currTerm += 1;
		return this.currTerm;
	}

	@Override
	public String toString() {
		return "NodeState [role=" + role + ", leaderId=" + leaderId + ", slefId=" + slefId + ", currTerm=" + currTerm
				+ ", currVoteFor=" + currVoteFor + ", nodeMap=" + nodeMap + ", currNodeAddress=" + currNodeAddress
				+ "]";
	}

}
